﻿using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;

namespace CRMG.Container.Source.Helper
{
    public static class StaticUtil
    {

        public static DateTime ToDateTime(this string str)
        {
            try
            {
                return DateTime.Parse(str);
            }
            catch (Exception)
            {
                return new DateTime();
            }
        }

        public static string GetColumnsAsString(this DataTable dt, params string[] columnList)
        {
            try
            {
                if (columnList == null)
                {
                    return string.Empty;
                }
                else
                {
                    StringBuilder strBuilder;
                    strBuilder = new StringBuilder();
                    foreach (DataRow row in dt.Rows)
                    {
                        foreach (var item in columnList)
                        {
                            strBuilder.AppendLine(row[item].ToStr());
                        }
                        strBuilder.AppendLine("/*********************************/");
                    }
                    return strBuilder.ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static Image ToImage(this object obj)
        {
            try
            {
                byte[] bt = (byte[])obj;
                return Image.FromStream(new MemoryStream(bt));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static string ToStr(this object obj)
        {
            try
            {
                if (object.Equals(obj, null) == true)
                {
                    return string.Empty;
                }
                else
                {
                    return obj.ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static int ToInt(this object obj)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static int Str2Int(this string str)
        {
            try
            {
                return Convert.ToInt32(str);
            }
            catch (Exception)
            {
                return 0;
            }
        }

    }
}
